/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlValue;

import org.openrdf.model.URI;

import be.destin.rdf.changes.Status;
import be.destin.rdf.changes.StatusInterface;

public class Notation implements URI, StatusInterface {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String scheme;
	private String notation;
	private Status status;
	
	public static String SESAME_NAMESPACE = "Notation:";

	public Notation() {
		super();
	}  
  
	public Notation(String list, String notation) {
		super();
		this.setScheme(list);
		this.setNotation(notation);
	}
	
	public Notation(String list_notation, Status aStatus) {
		super();
		this.status = aStatus;
		String anAbout = SkosUtil.normalizeAbout(list_notation);
		int i = anAbout.indexOf(SchemeInterface.separator);
		this.setScheme(anAbout.substring(0,i));
		this.setNotation(anAbout.substring(i+1));
	}
	
	public static Notation newValid(String list, String notation) {
		if (list == null) return null;
		if (notation == null) return null;
		list = SkosUtil.normalizeAbout(list);
		if (list.length()==0) return null;
		notation = SkosUtil.normalizeAbout(notation);
		if (notation.length()==0) return null;
		Notation newNotation = new Notation();
		newNotation.scheme = list;
		newNotation.notation = notation;
		return newNotation;
	}

	public Notation getThis() {
		return this;
	}

	/**
	 * @return the notation
	 */
	@XmlValue
	public String getNotation() {
		return this.notation;
	}

	/**
	 * @return the scheme
	 */
	@XmlAttribute(required=true)
	public String getScheme() {
		return scheme;
	}

	@XmlTransient
	public String getScheme_About() {
		return scheme+SchemeInterface.separator+notation;
	}

	/**
	 * @param notation the notation to set
	 */
	public void setNotation(String notation) {
		if (notation == null) this.notation = null;
		else {
			this.notation = SkosUtil.normalizeAbout(notation);
		}
	}

	/**
	 * @param scheme the scheme to set
	 */
	public void setScheme(String list) {
		if (list == null) this.scheme = null;
		else this.scheme = SkosUtil.normalizeAbout(list);
	}  
  
	@XmlAttribute(name="status")
	public String getStatus() {
		if (status == null) return null;
		return status.toString();
	}

	public void setStatus(String aStatus) {
		if (aStatus == null || aStatus.isEmpty()) {
			this.status = null;
			return;
		}
		if (aStatus.startsWith("status_")) {
			aStatus = aStatus.substring("status_".length());
		}
		this.status = Status.valueOf(aStatus);
	}

	@XmlTransient
	public Status getCurrentStatus() {
		return status;
	}

	public void setCurrentStatus(Status status) {
		if (status == null) return;
		this.status = status;
	}
	
	public String toRdfObjectUrl() {
		return "&notation="+this.getScheme_About();
	}

	public String toJavaScriptParameters() {
		return ",notation:'"+this.getScheme_About()+"'";
	}


	public String toString() {
		return getNamespace()+getScheme_About();
	}

	public String stringValue() {
		return getNamespace()+getScheme_About();
	}

	public String getNamespace() {
		return SESAME_NAMESPACE;
	}
	
	public String getLocalName() {
		return getScheme_About();
	}
	
	public boolean equals(Object o) {
		if (o == null) return false;
		if (this == o) return true;
		if ( ! (o instanceof Notation)) return false;
		return this.getScheme_About().equals(((Notation)o).getScheme_About());
	}
	
	public int hashCode() {
		String identifier = this.getScheme_About(); 
		if (identifier == null) return 0;
		return identifier.hashCode();
	}


}